//
//  PTSTabBarViewController.m
//  PTS80
//
//  Created by 黄杰 on 15/11/3.
//  Copyright © 2015年 黄杰. All rights reserved.
//

#import "PTSTabBarViewController.h"
#import "PTSBaseNavigationController.h"
#import "PTSBaseViewController.h"

@interface PTSTabBarViewController () <PTSBaseTabBarDelegate>

@end

@implementation PTSTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.openItemClick = YES;
    
    [self setupTabBar];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 移除自带的item
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
    
}

// 添加tabBar
- (void)setupTabBar
{
    PTSBaseTabBar *tabBarView = [[PTSBaseTabBar alloc] initWithFrame:self.tabBar.bounds];
    tabBarView.delegate = self;
    [self.tabBar addSubview:tabBarView];
    self.tabBarView = tabBarView;
}

- (void)setupTabBarBackgroundColor:(UIColor *)bgColor
{
    self.tabBarView.backgroundColor = bgColor;
}

- (void)setupSelectedItemColor:(UIColor *)itemColor
{
    [self.tabBarView setupSelectedItemColor:itemColor];
}

- (void)setupLineColor:(UIColor *)lineColor
{
    [self.tabBarView setupLineColor:lineColor];
}

- (void)setupTitleColor:(UIColor *)titleColor selectedColor:(UIColor *)selectedColor
{
    self.tabBarView.titleColor = titleColor;
    self.tabBarView.selectedColor = selectedColor;
}

/**
 *  设置子控件
 *
 *  @param titleArray       名称数组
 *  @param vcNameArray      控制器名称数组
 *  @param normalImgArray   普通状态图片数组
 *  @param selectedImgArray 选中状态图片数组
 */
- (void)setupSubViesWithTitleArray:(NSMutableArray *)titleArray VcNameArray:(NSMutableArray *)vcNameArray NormalImgArray:(NSMutableArray *)normalImgArray SelectedImgArray:(NSMutableArray *)selectedImgArray
{
    for (NSInteger i = 0; i < titleArray.count; i++) {
        [self setupChildView:vcNameArray[i] withTitle:titleArray[i] withImage:[UIImage imageNamed:normalImgArray[i]] withSelectedImage:[UIImage imageNamed:selectedImgArray[i]]];
    }
}

- (void)setupChildView:(PTSBaseViewController *)childView withTitle:(NSString *)title withImage:(UIImage *)image withSelectedImage:(UIImage *)selectedImage
{
    PTSBaseNavigationController *nav = [[PTSBaseNavigationController alloc] initWithRootViewController:childView];
    if (IOS7) {
        nav.navigationBar.frame = CGRectMake(0, 20, nav.navigationBar.frame.size.width, nav.navigationBar.frame.size.height);
    } else {
        nav.navigationBar.frame = CGRectMake(0, 0, nav.navigationBar.frame.size.width, nav.navigationBar.frame.size.height);
    }
    childView.tabBarItem.image = image;
    childView.tabBarItem.title = title;
    if (IOS7) {
        childView.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    } else {
        childView.tabBarItem.selectedImage = selectedImage;
    }
    childView.tabBarItem.selectedImage = selectedImage;
    [self addChildViewController:nav];
    
    [self.tabBarView addTabBarButtonWithItem:childView.tabBarItem];
}

#pragma mark - PTSBaseTabBarDelegate
- (void)baseTabBar:(PTSBaseTabBar *)baseTabBar getItemTag:(NSInteger)tag
{
//    if (tag == 0) {
//        if ([self.delegate2 respondsToSelector:@selector(baseTabBarController:getItemTag:)]) {
//            [self.delegate2 baseTabBarController:self getItemTag:tag];
//        }
//    } else {
//        self.selectedIndex = tag;
//    }
    
    if (self.openItemClick) {
        self.selectedIndex = tag;
    }
    
    if (self.clickCompleted) {
        self.clickCompleted(tag);
    }
    
}


@end
